$(function() {
	layui.use(['form', 'laydate', 'layer','util'], function() {
		var form = layui.form;
		var laydate = layui.laydate;
		var layer = layui.layer;
		 var util=layui.util;
		form.render();
		//重新加载date空间
		laydate.render({
			elem: "#birthday", //绑定文本框的id
			type: "date" //时间类型，年月日，时分秒
		});

		//截取地址栏中的参数
		function GetQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r != null) return unescape(r[2]);
			return null;
		}

		var userId = GetQueryString("userId");
		var version=null;
		console.log(userId);
		var  listUserRole=[];//获取用于所拥有的角色列表
        //通过用户Id查询用户详情
		function find_userInfo_byUserId(userId) {
         $.ajax({
         	type:"get",
         	url:"http://localhost:8080/ssmService/sysUser/selectUserVoByUserId/"+userId,
         	async:false,
         	"success":function(result){
         		if(result.resultCode==0){
         			build_userInfo(result);
         			listUserRole=result.data.listUserRole;
         		   version =result.data.user.version;
         		}
         		
         	}
         	
         });
         
		}
		function build_userInfo(result){
			var data=result.data;
			if(data.user.head_img_url!=""){
			$("#showImg").attr("src",data.user.head_img_url);
			}
			$("input[name='user.account']").val(data.user.account);
			$("input[name='user.name']").val(data.user.name);
			$("input[name='user.phone']").val(data.user.phone);
			var sel=["input[title='保密']","input[title='男']","input[title='女']"];
			$(sel[data.user.sex]).prop("checked",true);
			$("input[name='user.birthday']").val(util.toDateString(data.user.birthday,"yyyy-MM-dd"));
          form.render();
	   }
		
		//获取全部角色列表
		function find_RoleName_list(){
			$.ajax({
				type:"get",
				url:"http://localhost:8080/ssmService/role/selectRoleName",
				async:true,
				"success":function(result){
					if(result.resultCode==0){
						build_roleName_list(result,listUserRole);
					}
				}
			});
		}
		
		//渲染角色列表
		function build_roleName_list(result,listUserRole){
			console.log(listUserRole);
			var roleList=result.data;
			$.each(roleList, function(index,item) {
              var roleItem=$("<input type='checkbox' value='" + item.role_id + "' title='" + item.name + "' >");
              $(".roleList").append(roleItem);
			    for(var i=0;i<listUserRole.length;i++){
			    	if(item.role_id==listUserRole[i].fk_role_id){
			    		roleItem.prop("checked",true);
			    	}
			    }
			});
			   form.render();
		}
		
		
		//将角色列表中选中的添加name，以便传值到后台
		$(".updateUser").click(function(){
			check_role();
			var token=uuid(16,16);
		   $("#token").val(token);
		   var user_Id=parseInt(userId);
		   $("#userId").val(user_Id);
		   $("#version").val(version);
		});
		 
		form.on("submit(updateUser)",function(data){
			console.log(data.field);
			var params=data.field;
			var use=data.field.user_id;
			console.log(use);
			update_user(params);
			return false;
		});
		 //遍历角色列表，将选中的角色框添加name属性
		    function check_role(){
		    	var i=0;
		 	$("input[type='checkbox']").each(function(){
		 		if($(this).is(":checked")){
		 			$(this).attr("name","listUserRole["+i+"].fk_role_id");
		 			i++;
		 		}
		 		
		 	});
		 	form.render();
		 }
		    
		//修改用户的ajax
		function update_user(params){
			$.ajax({
				type:"post",
				url:"http://localhost:8080/ssmService/sysUser/updateUserVo",
				data:params,
				async:true,
				"success":function(result){
					if(result.resultCode==0){
						layer.msg(result.message,{
							icon:1,
							time:1000
						});
					
					//var index=parent.layer.getFrameIndex(window.name);
					parent.location.reload();
					//parent.layer.close(index);
					}
				}
				
			});
			
		}
		
		find_userInfo_byUserId(userId);
		find_RoleName_list();
		
		
 function uuid(len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  var uuid = [], i;
  radix = radix || chars.length;
 
  if (len) {
   // Compact form
   for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
  } else {
   // rfc4122, version 4 form
   var r;
 
   // rfc4122 requires these characters
   uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
   uuid[14] = '4';
 
   // Fill in random data. At i==19 set the high bits of clock sequence as
   // per rfc4122, sec. 4.1.5
   for (i = 0; i < 36; i++) {
    if (!uuid[i]) {
     r = 0 | Math.random()*16;
     uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
    }
   }
  }
 
  return uuid.join('');
}
	});

});